package com.yuju.controller.accounts;

import com.alibaba.fastjson.JSONObject;
import com.yuju.account.dao.entity.Shift;
import com.yuju.account.enums.ShiftReturnCodeEnum;
import com.yuju.account.model.ShiftAccountModel;
import com.yuju.account.model.ShiftModel;
import com.yuju.account.service.IShiftService;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.DepartmentReturnCodeEnum;
import com.yuju.order.enums.RoomStatusReturnCodeEnum;
import com.yuju.util.Utils;
import com.yuju.vo.account.ShiftVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 
 * 类名：ShiftController  <br />
 *
 * 功能：交班
 *
 * @author : jiangp <br />
 * @Date : 2017年12月26日 下午5:09:52  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("shift")
public class ShiftController extends BaseController<ShiftVO, ShiftModel> {
    @Autowired
    private IShiftService shiftService;

    private static Integer CURRENT_CLASS = 1;
    private static Integer PREVIOUS_CLASS = 2;

    /**
     * 
     * 功能：获得预交班信息<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:10:09 <br/>
     */
    @RequestMapping("pre")
    @ResponseBody
    public Result getShiftPre(@RequestBody JSONObject obj){
        String hotelId = obj.get("hotelId").toString();
        ShiftModel shiftModel = shiftService.getShift(hotelId);
        Result result = new Result();

        if (shiftModel != null) {
            result.setRows(null);
            result.setData(shiftModel);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_PRE_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_PRE_SUCCESS.getDesc());
        }
        else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_PRE_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_PRE_SUCCESS.getDesc());
        }
        return result;

    }

    /**
     * 
     * 功能：交班操作<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:10:25 <br/>
     */
    @RequestMapping("add")
    @ResponseBody
    public Result getShiftAdd(@RequestBody JSONObject obj){
        String hotelCode = obj.get("hotelCode").toString();
        ShiftModel shiftModel = obj.toJavaObject(ShiftModel.class);
        int row = shiftService.addShift(shiftModel.getGroupId(),
                shiftModel.getHotelId(), shiftModel.getShiftEmpId(), shiftModel.getShiftName(), hotelCode);
        Result result = new Result();

        if (result != null) {
            result.setRows(null);
            result.setData(row);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_ADD_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_ADD_SUCCESS.getDesc());
        }
        else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_ADD_FAILURE.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_ADD_FAILURE.getDesc());
        }
        return result;

    }

    /**
     * 
     * 功能：交班后，返回当前班次信息<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:10:46 <br/>
     */
    @RequestMapping("info")
    @ResponseBody
    public Result getShiftInfo(@RequestBody JSONObject obj){
        ShiftModel shiftModel = obj.toJavaObject(ShiftModel.class);
        ShiftModel shiftData = shiftService.findById(shiftModel.getId());
        Result result = new Result();

        if (shiftData != null) {
            result.setRows(shiftData.getShiftEmpModelList());
            result.setData(shiftData);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_INFO_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_INFO_SUCCESS.getDesc());
        }
        else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_INFO_FAILURE.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_INFO_FAILURE.getDesc());
        }
        return result;

    }

    /**
     * 
     * 功能：查询交班信息<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:11:11 <br/>
     */
    @RequestMapping("query")
    @ResponseBody
    public Result getShiftQuery(@RequestBody JSONObject obj){
        ShiftModel shiftModel = obj.toJavaObject(ShiftModel.class);
        List<ShiftModel> list = shiftService.queryShiftInfo(shiftModel);
        Result result = new Result();

        if (list != null) {
            result.setRows(list);
            result.setData(null);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_QUERY_IFNO_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_QUERY_IFNO_SUCCESS.getDesc());
        }
        else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_QUERY_IFNO_FAILURE.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_QUERY_IFNO_FAILURE.getDesc());
        }
        return result;

    }

    /**
     * 
     * 功能：查询交班账务信息<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:11:25 <br/>
     */
    @RequestMapping("account")
    @ResponseBody
    public Result getShiftAccount(@RequestBody JSONObject obj){
        ShiftAccountModel shiftAccountModel = obj.toJavaObject(ShiftAccountModel.class);
        List<ShiftAccountModel> list = shiftService.queryShiftAccountInfo(shiftAccountModel);
        Result result = new Result();

        if (list != null) {
            result.setRows(list);
            result.setData(null);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_QUERY_ACCOUNT_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_QUERY_ACCOUNT_SUCCESS.getDesc());
        }
        else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_QUERY_ACCOUNT_FAILURE.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_QUERY_ACCOUNT_FAILURE.getDesc());
        }
        return result;

    }

    /**
     * 
     * 功能：根据下拉列表，获得上班和本班基本信息<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:11:37 <br/>
     */
    @RequestMapping("getShiftCode")
    @ResponseBody
    public Result getShiftCode(@RequestBody JSONObject obj){
        String hotelId = obj.get("hotelId").toString();
        ShiftAccountModel shiftAccountModel = obj.toJavaObject(ShiftAccountModel.class);
        ShiftModel shiftModel = null;
        if (shiftAccountModel.getClassType().equals(CURRENT_CLASS)) {
            shiftModel = shiftService.getCurrentShiftBaseInfo(shiftAccountModel.getHotelId());
        }
        else if (shiftAccountModel.getClassType().equals(PREVIOUS_CLASS)) {
            shiftModel = shiftService.getPreviousShiftBaseInfo(shiftAccountModel.getHotelId());
        }
        Result result = new Result();

        if (shiftModel != null) {
            result.setRows(null);
            result.setData(shiftModel);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_SHIFT_CODE_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_SHIFT_CODE_SUCCESS.getDesc());
        }
        else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_SHIFT_CODE_FAILURE.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_SHIFT_CODE_FAILURE.getDesc());
        }
        return result;

    }

    /**
     * 
     * 功能：获得班次基本信息列表<br/>
     *
     * @author jiangp
     * @Date 2017年12月26日 下午5:12:04 <br/>
     */
    @RequestMapping("list")
    @ResponseBody
    public Result getShiftList(@RequestBody JSONObject obj){
        ShiftVO shiftVO = obj.toJavaObject(ShiftVO.class);

        Map<String, String> fail = FieldValidator.validateAll(shiftVO, "pager", true);
        if (!fail.isEmpty()) {
            return validateError(fail);
        }
        Result result = new Result();
        PageModel pageModel = shiftService.pageList(convert(shiftVO), shiftVO.getPage(), shiftVO.getPageSize());

        if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
            result.setPageModel(pageModel);
            result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
            result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
            result.setReturnCode(ShiftReturnCodeEnum.SHIFT_GET_SHIFT_CODE_SUCCESS.getCode());
            result.setReturnMsg(ShiftReturnCodeEnum.SHIFT_GET_SHIFT_CODE_SUCCESS.getDesc());
        } else {
            result.setStatus(ReturnStatusEnum.FAILURE.getCode());
            result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
            result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
            result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
        }
        return result;

    }

}
